import {
  ProjectContactGetCheckedCompany,
  ProjectContactAdd,
  ProjectContactDelete,
  ProjectContactEdit,
  ProjectContactCommitStatus,
  ProjectContactAuditEdit,
} from '@/services/municipal';

export default {
  namespace: 'ProjectContact',
  state: {
    listAndTotal: null,
    record: null,
  },

  effects: {
    *fetchList({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactGetCheckedCompany, payload);

      if (response.data) {
        yield put({
          type: 'listAndTotal',
          payload: response,
        });
      } else {
        if (callback) callback(response);
      }
    },

    *fetchProjectContactAdd({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactAdd, payload);

      if (callback) callback(response);
    },
    *fetchProjectContactEdit({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactEdit, payload);

      if (callback) callback(response);
    },
    *fetchProjectContactCommitStatus({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactCommitStatus, payload);
      if (callback) callback(response);
    },
    *fetchProjectContactAuditEdit({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactAuditEdit, payload);
      if (callback) callback(response);
    },
    *fetchProjectContactDelete({ payload, callback }, { call, put }) {
      const response = yield call(ProjectContactDelete, payload);
      if (callback) callback(response);
    },
    *fetchSaveRecord({ payload, callback }, { call, put }) {
      yield put({
        type: 'record',
        payload: payload,
      });
    },
  },

  reducers: {
    listAndTotal(state, action) {
      return {
        ...state,
        listAndTotal: action.payload,
      };
    },
    record(state, action) {
      return {
        ...state,
        record: action.payload,
      };
    },
  },
};
